/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **
* Author : Prasad J Ghangal					*
* Title : Job Scheduling-Priority Non Preem				*
* Algorithm : -							*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
class abc
{
    public:
	int at,bt,ft,fg,prio;
	abc(int a,int b,int c)
	{
		at=a;
		bt=b;
		prio=c;
		ft=0;
		fg=0;
	}
};

bool cmp(abc a,abc b)
{
	return a.at<b.at;
}

int main()
{
	vector<abc> v;
	int n=5,a,b,i,c;
	cout<<"Enter the AT and BT of the processes\n";
	for(i=0;i<n;i++)
	{
		cin>>a>>b>>c;
		v.push_back(abc(a,b,c));
	}
	sort(v.begin(),v.end(),cmp); 	//sort acc. to at

	int ct=0,j,k,min1;
	for(i=0;i<n;i++)
	{
		if(ct>=v[i].at)
		{
			min1=99999;
			for(j=0;j<n;j++)
			{
				if(min1>=v[j].prio && v[j].at<=ct && v[j].fg==0)	//find shortest prio
				{
					min1=v[j].prio;
					k=j;
				}
			}
			v[k].ft=ct+v[k].bt;
			ct=v[k].ft;
			v[k].fg=1;
		}
		else
		{
			ct++;
			i--;
		}
	}

	cout<<"Pid\tAT\tBT\tFT\tTT\tWT\n";

	for(i=0;i<n;i++)
	{
		int tt=v[i].ft-v[i].at;
		cout<<i<<"\t"<<v[i].at<<"\t"<<v[i].bt<<"\t"<<v[i].ft<<"\t"<<tt<<"\t"<<tt-v[i].bt<<"\n";
	}

	return 0;
}
